package com.zj.healthmanage.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zj.healthmanage.constant.RedisConstant;
import com.zj.healthmanage.entity.PageResult;
import com.zj.healthmanage.entity.QueryPageBean;
import com.zj.healthmanage.pojo.Checkgroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.zj.healthmanage.pojo.Address;
import com.zj.healthmanage.mapper.AddressMapper;
import com.zj.healthmanage.service.AddressService;
import redis.clients.jedis.JedisPool;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService{

    @Resource
    private AddressMapper addressMapper;

    @Resource
    private JedisPool jedisPool;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return addressMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Address record) {
        return addressMapper.insert(record);
    }

    @Override
    public int insertSelective(Address record) {
        String img = record.getImage();
        if (img != null){
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,img);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,img);
        }
        return addressMapper.insertSelective(record);
    }

    @Override
    public Address selectByPrimaryKey(Integer id) {
        return addressMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Address record) {
        return addressMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Address record) {
        return addressMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageResult queryAddressByPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Address> page = addressMapper.queryAddressByPage(queryString);
        long total = page.getTotal();
        List<Address> rows = page.getResult();
        return new PageResult(total,rows);
    }

    @Override
    public Address queryAddressById(Integer id) {
        return  addressMapper.queryAddressById(id);
    }

}
